package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NeProdoperElectricEff;
import com.nari.ncps.screen.mapper.ne.NeProdoperElectricEffMapper;
import com.nari.ncps.screen.service.ne.NeProdoperElectricEffService;
import com.nari.ncps.screen.vo.ne.NeElectricEfficiencyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 电力生产效率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeProdoperElectricEffServiceImpl extends ServiceImpl<NeProdoperElectricEffMapper, NeProdoperElectricEff> implements NeProdoperElectricEffService {

    @Autowired
    private NeProdoperElectricEffMapper neProdoperElectricEffMapper;

    @Override
    public List<NeElectricEfficiencyVO> getElectricProductionEfficiency(Integer year) {
// 获取当前日期
        LocalDate currentDate = LocalDate.now();
// 获取七天前的日期
        LocalDate sevenDaysAgo = currentDate.minusDays(6); // 包含当天，共7天

// 格式化为 yyyyMMdd
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String current = currentDate.format(formatter);
        String sevenDaysAgoStr = sevenDaysAgo.format(formatter);

        // 查询近7天的日度电力生产效率数据（date_type = 2）
        // 查询最近7天的数据
        LambdaQueryWrapper<NeProdoperElectricEff> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(NeProdoperElectricEff::getOrgCode, "100360").eq(NeProdoperElectricEff::getDateType, "2")
                .ge(NeProdoperElectricEff::getStatisticTime, sevenDaysAgoStr)
                .le(NeProdoperElectricEff::getStatisticTime, current);

        List<NeProdoperElectricEff> dataList = this.list(wrapper);

        // 如果数据为空，返回空列表
        if (dataList == null || dataList.isEmpty()) {
            return new ArrayList<>();
        }

        // 将查询结果转换为VO对象
        return dataList.stream().map(data -> {
            NeElectricEfficiencyVO vo = new NeElectricEfficiencyVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());
            vo.setDay(data.getDay());
            vo.setDate(data.getDate());
            vo.setFanOperationHour(data.getFanOperationHour());
            vo.setPvOperationHour(data.getPvOperationHourme());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取过去7天内的日期数组
     * @return  日期数组
     */
    public static ArrayList<String> pastDay(String time){
        ArrayList<String> pastDaysList = new ArrayList<>();
        try {
            //我这里传来的时间是个string类型的，所以要先转为date类型的。
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(time);
            for (int i = 6; i >= 0; i--) {

                pastDaysList.add(getPastDate(i,date));
            }
        }catch (ParseException e){
            e.printStackTrace();
        }
        return pastDaysList;
    }


    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String result = sdf.format(today);
        System.out.println(result);
        return result;
    }



}
